// packageG/gift_card/channel/channel.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 请求参数对象
    queryObj:{
      page: 1,
      page_size: 5
    },
    // 总数量，用来实现分页
    total: 0,
    // 是否正在请求数据
    isloading: false,
    finished: false,
    giftCard_data: [],
    // 礼品卡状态数据枚举
    giftCard_statusEnum: {
      1: '未激活',
      2: '已激活',
      3: '已绑定',
      4: '已使用',
      5: '已过期',
      6: '作废'
    },
    // 绑定渠道
    bindChannelsShow: false,
    currentGiftCard: {},
    channelsKeywords: '',
    channersList: [],
    selectedChannel: '',
    loadingShow: false,
    loadingText: '加载中...'
  },

  /**
   * 获取礼品卡列表
   * @param {Function} callBack 回调函数(停止页面下拉)
   */
  getGiftcardData: function(callBack) {
    const { queryObj } = this.data
    let data = {...queryObj}
    const urlStr = app.getNetAddresss('plugin.gift-card.api.record.sellerList')
    const _this = this
    this.setData({ loadingShow: true })
    app._postNetWork({
      url: urlStr,
      data,
      success: function(res) {
        const resData = res.data
        if (resData.result == 1) {
          const resList = resData.data.data
          const { total } = resData.data
          _this.setData({
            total,
            giftCard_data: [..._this.data.giftCard_data, ...resList],
            finished: _this.data.queryObj.page * _this.data.queryObj.page_size >= total
          })
        } else {
          app.tips(resData.msg || '数据获取失败！')
        }
      },
      complete: function() {
        _this.setData({ loadingShow: false })
        callBack && callBack()
      }
    })
  },

  /**
   * 显示绑定渠道
   */
  showBindChannels: function(e) {
    const { datas } = e.currentTarget.dataset
    if (!datas.is_bind) {
      return
    }
    this.setData({ 
      bindChannelsShow: true,
      currentGiftCard: datas
    })
  },

  /**
   * 关闭绑定渠道
   */
  closeChannels: function() {
    this.setData({
      channelsKeywords: '',
      channersList: [],
      bindChannelsShow: false,
      selectedChannel: '',
      currentGiftCard: {}
    })
  },

  /**
   * 输入框数据双向绑定
   * @param {Object} e 
   */
  keywordsInput: function(e) {
    this.setData({
      channelsKeywords: e.detail
    })
  },

  /**
   * 搜索渠道
   */
  searchChannels: function() {
    const { channelsKeywords } = this.data
    if (!channelsKeywords) {
      app.tips('请输入会员id/昵称/手机')
      return
    }
    const urlStr = app.getNetAddresss('plugin.gift-card.api.record.getMemberList')
    const _this = this
    _this.setData({ channersList: [], loadingShow: true })
    app._getNetWork({
      url: urlStr,
      data: { keyword: channelsKeywords },
      success: function(res) {
        const resData = res.data
        if (resData.result == 1) {
          _this.setData({ channersList: resData.data })
        } else {
          app.tips(resData.msg || '获取失败！')
        }
      },
      complete: function() {
        _this.setData({ loadingShow :false })
      }
    })
  },

  /**
   * 渠道选中
   * @param {Object} e 
   */
  channelSelect: function(e) {
    const { detail } = e
    this.setData({ selectedChannel: detail })
  },

  /**
   * 绑定渠道
   */
  bindChnannel: function() {
    const { selectedChannel, currentGiftCard } = this.data
    if (!selectedChannel) {
      app.tips('请选择要绑定的渠道')
      return
    }
    const urlStr = app.getNetAddresss('plugin.gift-card.api.record.bindChannelMember')
    const _this = this
    this.setData({ loadingShow: true })
    app._postNetWork({
      url: urlStr,
      data: {id: currentGiftCard.id,uid: selectedChannel},
      success: function(res) {
        const resData = res.data
        if (resData.result == 1) {
          wx.showModal({
            title: '成功',
            content: resData.msg || '绑定成功！',
            showCancel: false,
            complete: (resC) => {
              if (resC.confirm) {
                _this.closeChannels()
                _this.setData({
                  'queryObj.page': 1,
                  total : 0,
                  isloading : false,
                  giftCard_data : [],
                  finished: false
                })
                _this.getGiftcardData()
              }
            }
          })
        } else {
          app.tips(resData.msg || '绑定失败！')
        }
      },
      complete: function() {
        _this.setData({ loadingShow: false })
      }
    })
  },

  /**
   * 未激活的礼品卡跳转至激活页面
   * @param {Object} e 
   */
  goActivate: function(e) {
    console.log(e)
    const { datas } = e.currentTarget.dataset
    const { status, id, code_sn, code_id, face_value } = datas
    // 卡片已激活过
    if (status != 1) {
      return
    }
    wx.navigateTo({
      url: `/packageG/gift_card/index/index?option_type=1&record_id=${id}&code_sn=${code_sn}&code_id=${code_id}&face_value=${face_value}`,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      'queryObj.page': 1,
      total : 0,
      isloading : false,
      giftCard_data : [],
      finished: false
    })
    this.getGiftcardData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      'queryObj.page': 1,
      total : 0,
      isloading : false,
      giftCard_data : [],
      finished: false
    })
    this.getGiftcardData(() => wx.stopPullDownRefresh())
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.queryObj.page * this.data.queryObj.page_size >= this.data.total){
      wx.showLoading({
        title: '数据加载完毕！',
      })
      wx.hideLoading() // 关闭loading
      return
    }
    if(this.data.isLoading) return
    this.setData({
      // 让页码值自增 +1
      page: this.data.queryObj.page += 1
    })
    // 重新获取列表数据
    this.getGiftcardData()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})